//增删改查
const Goods = require("../models/goodsModel");
// const CompanyMaterial = require("../models/companyMaterialModel");
const Calculation = require("../util/calculation");
const mongooes = require("mongoose");

exports.createGoods = async (req, res) => {
  try {
    const company_id = String(req.user.company_id);
    const item = req.body;
    item.companyId = company_id;
    // console.log(item);
    const newGoods = await Goods.create(item);
    const newgoods = await Goods.findOne({ _id: newGoods._id })
      .populate("baseplate")
      .populate("shroudplate");
    // 201 Created 已创建
    res.status(201).json({
      status: "success",
      data: newgoods,
    });
  } catch (err) {
    console.log("添加商品成功", err);

    res.status(404).json({
      Error: err,
    });
  }
};

// exports.getAllGoods = async (req, res) => {
//   try {
//     const goods = await Goods.find();
//     // OK 请求成功
//     res.status(200).json({
//       status: "success",
//       data: goods,
//     });
//   } catch (err) {
//     console.log(err);
//     res.status(404).json({
//       Error: err,
//     });
//   }
// };
//获取某个公司下的商品
exports.getGoodsByCompanyId = async (req, res) => {
  try {
    const company_id = String(req.user.company_id);

    const goods = await Goods.find({ companyId: company_id })
      .populate("baseplate")
      .populate("shroudplate");

    res.status(200).json({
      status: "success",
      data: goods,
    });
  } catch (error) {
    res.status(404).json({
      Error: error,
    });
  }
};

exports.updateGoods = async (req, res) => {
  try {
    const company_id = String(req.user.company_id);
    const item = req.body;
    item.companyId = company_id;

    const goods = await Goods.findByIdAndUpdate(
      mongooes.Types.ObjectId(req.params.id),
      item,
      {
        new: true,
      }
    );

    const newgoods = await Goods.findOne({ _id: goods._id })
      .populate("baseplate")
      .populate("shroudplate");

    if (!goods) {
      return res.status(404).json({
        message: "该商品并不存在",
      });
    }
    res.status(200).json({
      status: "success",
      message: newgoods,
    });
    console.log("修改商品成功");
  } catch (err) {
    console.log("修改商品失败", err);
    res.status(404).json({
      err,
    });
  }
};

exports.deleteGoods = async (req, res) => {
  try {
    const id = mongooes.Types.ObjectId(req.params);
    const data = await Goods.deleteOne({ _id: id });
    res.status(200).json({
      status: "success",
      data: data,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      Error: err,
    });
  }
};

exports.calculate = async (req, res) => {
  let reqData = req.body;
  // console.log(req.body);
  Goods.find()
    .populate("baseplate")
    .populate("shroudplate")
    .populate("companyId")
    .exec(function (err, good) {
      // if (err) return handleError(err)
      if (err) {
        console.log(err);
        res.status(404).json({
          Error: err,
        });
      } else {
        let goods = Calculation.calculatePrice(reqData, good);
        // console.log("goods----", goods);
        res.status(200).json({
          status: "success",
          // data: priceDetails,
          data: goods,
        });
      }
    });
  // console.log(goods);
  // res.status(200).json({
  //   status: "success",
  //   data: goods,
  // });
};
